package com.shop.admin.controller;

import com.shop.admin.entity.ShopCategory;
import com.shop.admin.result.ApiResult;
import com.shop.admin.result.ApiResultList;
import com.shop.admin.service.ShopCategoryService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.valid.AddGroup;
import com.shop.admin.valid.UpdateGroup;
import com.shop.admin.vo.CateOrderListVo;
import com.shop.admin.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = {"ShopCategoryController"},description = "分类页面所需接口")
@RestController
@RequestMapping("/api/category")
public class ShopCategoryController {

    @Resource
    private ShopCategoryService shopCategoryService;


    //分类查询
    @GetMapping("/list")
    @ApiOperation(value = "获取组织所有分类",notes = "page,limit,orgId")
    public ApiResult list(@RequestParam Map<String, Object> params){
        PageUtils page = shopCategoryService.queryPage(params);
         return ApiResult.ok(page);
    }

//    //分类vo查询
//    @GetMapping("/catelog")
//    @ApiOperation(value = "商品系统获取分类",notes = "无需传参")
//    public ApiResultList catelog(@RequestParam Map<String, Object> params){
//        List<CategoryVo> vos = shopCategoryService.queryPageByCate(params);
//        return ApiResultList.ok(vos);
//    }

    //新增分类
    @PostMapping("/save")
    @ApiOperation(value = "新增分类",notes = "分类名字,url，组织id")
    public ApiResult save(@Validated(AddGroup.class) @ApiParam("参数为：cateName,cateUrl,orgId") @RequestBody ShopCategory category){
        if (shopCategoryService.selectBoolean(category.getCateName(),category.getOrgId())){
            category.setCateName(category.getCateName());
            category.setCateUrl(category.getCateUrl());
            category.setCreateTime(new Date());
            category.setUpdateTime(new Date());
            category.setOrgId(category.getOrgId());
            shopCategoryService.save(category);
            return ApiResult.ok("新增成功！");
        }
        return ApiResult.ok("分类重复");
    }

    //修改分类
    @PostMapping("/update")
    @ApiOperation(value = "分类修改",notes = "分类id,分类名字,cateUrl")
    public ApiResult updateSort(@Validated(UpdateGroup.class) @ApiParam("参数为：cateId,cateName,cateUrl") @RequestBody ShopCategory category){
        if (shopCategoryService.selectBoolean(category.getCateName(),category.getOrgId())){
            shopCategoryService.updateCascade(category);
            return ApiResult.ok("修改成功！");
        }
        return ApiResult.ok("分类重复");
    }

    //删除分类
    @PostMapping("/delete")
    @ApiOperation(value = "分类删除",notes = "分类id,id可为数组类型")
    public ApiResult updateSort(@ApiParam("参数为：cateId[]") @RequestBody Long[] ids){
        shopCategoryService.removeCategoryByIds(Arrays.asList(ids));
        return ApiResult.ok("删除成功！");
    }

//    //商品关联
//    @GetMapping("/cate/list")
//    @ApiOperation(value = "商品关联",notes = "分类id")
//    public ApiResultList catelogList(@ApiParam("参数为：cateId") @RequestParam("cateId") Long cateId){
//        List<CateOrderListVo> data  = shopCategoryService.selectOrderList(cateId);
//        return ApiResultList.ok(data);
//    }
}
